package com.erpanderp.wam.ac.view.project;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.component.selectonemenu.SelectOneMenu;
import org.primefaces.context.RequestContext;

import com.erpanderp.wam.ac.model.entity.project.ProjectAssignment;
import com.erpanderp.wam.ac.model.entity.project.ProjectAssignmentDetail;
import com.erpanderp.wam.ac.model.service.project.ProjectAssignmentDAO;
import com.erpanderp.wam.ac.model.service.project.ProjectAssignmentDetailDAO;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.PassportAndPANDetails;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.projectandassignment.ProjectDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;
@ManagedBean
@ViewScoped
public class InternationalProjectAssignmentsListBean implements Serializable {


	private static final long serialVersionUID = 1L;
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);
	
	ProjectDAO projectDAO = new ProjectDAO();
	ProjectAssignmentDAO projectAssignmentDAO = new ProjectAssignmentDAO();
	ProjectAssignmentDetailDAO projectAssignmentDetailDAO = new ProjectAssignmentDetailDAO();
	
	private ProjectAssignment projectAssignment = new ProjectAssignment();
	private ProjectAssignmentDetail projectAssignmentDetail = new ProjectAssignmentDetail();
	private Employee employee = new Employee();
	private Project selectedProject= new Project();
	private WAMUser wamUser = (WAMUser) session.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER);
	private Employee reportingManager;
	
	private boolean SHOW_EDIT_DIALOG=false;
	private boolean FORM_VALID;
	private boolean renderApproveReject;
	private boolean renderUpdate;
	private boolean renderClose;
	private boolean renderProjectsAssignmentsList;
	private boolean renderProjectsAssignments;
	
	private List<ProjectAssignment> projectAssignmentsList = new ArrayList<ProjectAssignment>();
	private List<ProjectAssignmentDetail> projectAssignmentDetailList = new ArrayList<ProjectAssignmentDetail>();
	private List<PassportAndPANDetails> passportAndPANDetailsList = new ArrayList<PassportAndPANDetails>();
	
	private List<Project> projects = new ArrayList<Project>();

    Map<String, Long> projectsMap = new HashMap<String, Long>();
	
	private Long projectAssignmentId;
	private Long selectedProjectId;
	
	private String projectAssignmentCode;
	private String employeeId;
	private String contactNumber="";
	private String passportNumber;
	private String approvedBy="";
	private Float advanceAmount;
	private Float roomRentAdvance;
	private Date createDate;
	private String additionals;
	
	public InternationalProjectAssignmentsListBean() 
	{
		// TODO Auto-generated constructor stub
		System.out.println("constructor start.................");
		setRenderProjectsAssignments(false);
		//HR and VP can view all
		//PM can view only those he can approve
		//EMP can view only his request.
		projectAssignment.setProject(new Project());
		projectAssignmentsList = projectAssignmentDAO.getProjectAssignmentsByEmployeeIdProjectType(wamUser.getEmployee().getEmployeeId(),CommonLookup.PROJECT_ASSIGNMENT_TYPE_INTERNATIONAL);
		System.out.println("constructor end...................");
	}
	
	public String viewProjectAssignmentInEditModeAction()
	{
		System.out.println("viewProjectAssignmentInEditMode");
		
		
		 System.out.println("===== viewProjectAssignmentInEditModeAction method=======");
         try
         {
        	 
          Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
          String projectAssignmentId = params.get("projectAssignmentId");
          String employeeId = params.get("employeeId");
          FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("projectAssignmentId", projectAssignmentId);
          FacesContext.getCurrentInstance().getExternalContext().getRequestMap().put("employeeId", employeeId);
          FacesContext.getCurrentInstance().getExternalContext().redirect("/wam/ac/project/editviewinternationalprojectassignment.html?projectAssignmentId="+projectAssignmentId+"&employeeId="+employeeId);
          System.out.println("projectAssignmentId ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("projectAssignmentId"));
          System.out.println("employeeId ==" + FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("employeeId"));
         
         }
         catch (Exception e) 
         {
              System.out.println("INSIDE CATCH EXCPTION:::::::::"+e);
              e.printStackTrace();
         }
         return "vieweditinternationalprojectassignment";
		}
	
	public void dialogClose(){	
		System.out.println("dialogClose .................");
		RequestContext.getCurrentInstance().execute("EditProjectAssignmentDialog.hide();");		
	}
	
	public void projectChangeListener(AjaxBehaviorEvent e) {
		
		
	    selectedProjectId = Long.parseLong(((SelectOneMenu)e.getComponent()).getValue().toString());
		System.out.println(selectedProjectId);
		projectAssignment.setProject(projectDAO.getProjectByProjectID(selectedProjectId));
     }
	

	
	public String updateDomesticProjectAssignment() 
	{
		System.out.println("In  updateDomesticProjectAssignment");
	    validateForm();
		if(FORM_VALID){
			projectAssignment.setApprovalStatus(CommonLookup.PROJECT_ASSIGNMENT_INPROCESS_STATUS);
			projectAssignmentDetail.setApprovalStatus(CommonLookup.PROJECT_ASSIGNMENT_INPROCESS_STATUS);
 			projectAssignment.setRecommendationStatus(CommonLookup.PROJECT_ASSIGNMENT_INPROCESS_STATUS);
			projectAssignmentDetail.setRecommendationStatus(CommonLookup.PROJECT_ASSIGNMENT_INPROCESS_STATUS);
		    projectAssignment.setStatus(CommonLookup.STATUS_ACTIVE);
			projectAssignmentDetail.setStatus(CommonLookup.STATUS_ACTIVE);
			projectAssignment.setModifiedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
			projectAssignmentDetail.setModifiedBy(wamUser.getEmployee().getFirstName() +" "+ wamUser.getEmployee().getLastName());
			projectAssignment.setDateModified(new Date());
			projectAssignmentDetail.setDateModified(new Date());
			
			if(projectAssignmentDAO.updateProjectAssignment(projectAssignment) != null ){
 			System.out.println("In update success....");
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, projectAssignment.getProjectAssignmentId() + " ProjectAssignment updated successfully.", ""));
			System.out.println("In update success....after");
			projectAssignment = new ProjectAssignment();
			projectAssignment.setProject(new Project());
			}else{
 				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Failed to update ProjectAssignment.", ""));
 			}
			RequestContext.getCurrentInstance().execute("EditProjectAssignmentDialog.hide();");
						

		}
		
		else{
			RequestContext.getCurrentInstance().execute("EditProjectAssignmentDialog.show();");			
		}
		
	
		return null;
	}
	
	private void validateForm(){
	System.out.println("validateForm.....................");
	FORM_VALID = true;
	if(ValidationUtil.isEmpty(projectAssignment.getEmergencycontactNumber(), "Emergency Contact Number ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	
	if(ValidationUtil.isEmpty(String.valueOf(projectAssignmentDetail.getDailyAllowance()), "Daily Allowance(INR) ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	if(ValidationUtil.isEmpty(String.valueOf(projectAssignmentDetail.getAdvanceAmount()), "Advance Amount(INR) ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	if(ValidationUtil.isEmpty(projectAssignmentDetail.getAdvanceByDate() != null ? projectAssignmentDetail.getAdvanceByDate().toString() :null , "Advance Amount By Date ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	if(ValidationUtil.isEmpty(projectAssignmentDetail.getPaymentCycleDate() != null ? projectAssignmentDetail.getPaymentCycleDate().toString() :null , "Payment Cycle [Date of every month] ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	if(ValidationUtil.isEmpty(String.valueOf(projectAssignmentDetail.getAdditionals()), "Additionals ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	if(ValidationUtil.isEmpty(String.valueOf(projectAssignmentDetail.getRoomRentAdvance()), "Room Rent Advance ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	if(ValidationUtil.isEmpty(String.valueOf(projectAssignmentDetail.getRoomRentPerMonth()), "Room Rent Per Month ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	if(ValidationUtil.isEmpty(String.valueOf(projectAssignmentDetail.getTotalAdvance()), "Total Amount ", null, FacesContext.getCurrentInstance()) != null){
		FORM_VALID = false;
	}
	
	System.out.println("Finally FORM_VALID is :::::"+FORM_VALID);
	}
	
	
	//GETTERS AND SETTERS
	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public ProjectAssignmentDAO getProjectAssignmentDAO() {
		return projectAssignmentDAO;
	}

	public void setProjectAssignmentDAO(ProjectAssignmentDAO projectAssignmentDAO) {
		this.projectAssignmentDAO = projectAssignmentDAO;
	}

	public Long getProjectAssignmentId() {
		return projectAssignmentId;
	}

	public void setProjectAssignmentId(Long projectAssignmentId) {
		this.projectAssignmentId = projectAssignmentId;
	}

	public List<ProjectAssignment> getProjectAssignmentsList() {
		return projectAssignmentsList;
	}

	public void setProjectAssignmentsList(List<ProjectAssignment> projectAssignmentsList) {
		this.projectAssignmentsList = projectAssignmentsList;
	}


	public ProjectAssignment getProjectAssignment() {
		return projectAssignment;
	}

	public void setProjectAssignment(ProjectAssignment projectAssignment) {
		this.projectAssignment = projectAssignment;
	}

	public ProjectAssignmentDetail getProjectAssignmentDetail() {
		return projectAssignmentDetail;
	}

	public void setProjectAssignmentDetail(ProjectAssignmentDetail projectAssignmentDetail) {
		this.projectAssignmentDetail = projectAssignmentDetail;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}


	public boolean isRenderProjectsAssignmentsList() {
		return renderProjectsAssignmentsList;
	}

	public void setRenderProjectsAssignmentsList(
			boolean renderProjectsAssignmentsList) {
		this.renderProjectsAssignmentsList = renderProjectsAssignmentsList;
	}

	public boolean isRenderProjectsAssignments() {
		return renderProjectsAssignments;
	}

	public void setRenderProjectsAssignments(boolean renderProjectsAssignments) {
		this.renderProjectsAssignments = renderProjectsAssignments;
	}

	public List<ProjectAssignmentDetail> getProjectAssignmentDetailList() {
		return projectAssignmentDetailList;
	}

	public void setProjectAssignmentDetailList(
			List<ProjectAssignmentDetail> projectAssignmentDetailList) {
		this.projectAssignmentDetailList = projectAssignmentDetailList;
	}

	public String getEmployeeId() {
		return employeeId;
	}

	public void setEmployeeId(String employeeId) {
		this.employeeId = employeeId;
	}
	public boolean isSHOW_EDIT_DIALOG() {
		return SHOW_EDIT_DIALOG;
	}
	public void setSHOW_EDIT_DIALOG(boolean sHOW_EDIT_DIALOG) {
		SHOW_EDIT_DIALOG = sHOW_EDIT_DIALOG;
	}

	public boolean isFORM_VALID() {
		return FORM_VALID;
	}




	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}




	public Project getSelectedProject() {
		return selectedProject;
	}




	public void setSelectedProject(Project selectedProject) {
		this.selectedProject = selectedProject;
	}




	public WAMUser getWamUser() {
		return wamUser;
	}




	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}




	public String getProjectAssignmentCode() {
		return projectAssignmentCode;
	}




	public void setProjectAssignmentCode(String projectAssignmentCode) {
		this.projectAssignmentCode = projectAssignmentCode;
	}




	public String getContactNumber() {
		return contactNumber;
	}




	public void setContactNumber(String contactNumber) {
		this.contactNumber = contactNumber;
	}




	public String getApprovedBy() {
		return approvedBy;
	}




	public void setApprovedBy(String approvedBy) {
		this.approvedBy = approvedBy;
	}




	public Float getAdvanceAmount() {
		return advanceAmount;
	}




	public void setAdvanceAmount(Float advanceAmount) {
		this.advanceAmount = advanceAmount;
	}




	public Float getRoomRentAdvance() {
		return roomRentAdvance;
	}




	public void setRoomRentAdvance(Float roomRentAdvance) {
		this.roomRentAdvance = roomRentAdvance;
	}




	public Date getCreateDate() {
		return createDate;
	}




	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}




	public Employee getReportingManager() {
		return reportingManager;
	}




	public void setReportingManager(Employee reportingManager) {
		this.reportingManager = reportingManager;
	}




	public String getAdditionals() {
		return additionals;
	}




	public void setAdditionals(String additionals) {
		this.additionals = additionals;
	}


	public boolean isRenderApproveReject() {
		return renderApproveReject;
	}


	public void setRenderApproveReject(boolean renderApproveReject) {
		this.renderApproveReject = renderApproveReject;
	}

	public Long getSelectedProjectId() {
		return selectedProjectId;
	}

	public void setSelectedProjectId(Long selectedProjectId) {
		this.selectedProjectId = selectedProjectId;
	}
	public ProjectAssignmentDetailDAO getProjectAssignmentDetailDAO() {
		return projectAssignmentDetailDAO;
	}

	public void setProjectAssignmentDetailDAO(
			ProjectAssignmentDetailDAO projectAssignmentDetailDAO) {
		this.projectAssignmentDetailDAO = projectAssignmentDetailDAO;
	}

	public List<Project> getProjects() {
		return projects;
	}

	public void setProjects(List<Project> projects) {
		this.projects = projects;
	}

	public Map<String, Long> getProjectsMap() {
		return projectsMap;
	}

	public void setProjectsMap(Map<String, Long> projectsMap) {
		this.projectsMap = projectsMap;
	}

	public boolean isRenderUpdate() {
		return renderUpdate;
	}

	public void setRenderUpdate(boolean renderUpdate) {
		this.renderUpdate = renderUpdate;
	}

	public boolean isRenderClose() {
		return renderClose;
	}

	public void setRenderClose(boolean renderClose) {
		this.renderClose = renderClose;
	}

	public String getPassportNumber() {
		return passportNumber;
	}

	public void setPassportNumber(String passportNumber) {
		this.passportNumber = passportNumber;
	}
	public List<PassportAndPANDetails> getPassportAndPANDetailsList() {
		return passportAndPANDetailsList;
	}

	public void setPassportAndPANDetailsList(
			List<PassportAndPANDetails> passportAndPANDetailsList) {
		this.passportAndPANDetailsList = passportAndPANDetailsList;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}


}
